# -*- coding: utf-8 -*-
from google.appengine.ext import db
from django.contrib.auth.models import User
from django.forms import ModelForm
from django.core.paginator import Paginator, InvalidPage, EmptyPage

class BlogCategory(db.Model):
    category = db.StringProperty(required=True)
    rootcategory = db.SelfReferenceProperty(required=False)
    description = db.StringProperty(required=False)
    displayorder = db.IntegerProperty(required=True, default=0)
    active = db.BooleanProperty(required=True, default=True)
    visited = db.IntegerProperty(required=True, default=0)
    created = db.DateTimeProperty(auto_now=False, auto_now_add=True)
    
    def __unicode__(self):
        return self.category
        
    @classmethod
    def get_category(cls):
        gategorylist = cls.all().filter('active =', True).order("-displayorder")
        return gategorylist
        
    @classmethod
    def get_by_key(cls, key):
        category = cls.get(key)
        category.visited = category.visited + 1
        category.put()
        return category
    
class Blog(db.Model):
    title = db.StringProperty(required=True)
    content = db.TextProperty(required=True)
    category = db.ReferenceProperty(BlogCategory, required=True)
    displayorder = db.IntegerProperty(required=True, default=0)
    active = db.BooleanProperty(required=True, default=True)
    sender = db.ReferenceProperty(User, required=True, collection_name='blog_set')
    visited = db.IntegerProperty(required=True, default=0)
    created = db.DateTimeProperty(auto_now=False, auto_now_add=True)
    
    def __unicode__(self):
        return self.title
        
    @classmethod
    def get_blog_list(cls, category = None, page = 1):
        query = cls.all()
        if category:
            blog = query.filter('active =', True)
            blog = blog.filter('category =', category)
            blog = blog.order("-displayorder")
            blog = blog.order("-created")
        else:
            blog = query.filter('active =', True)
            blog = blog.order("-displayorder")
            blog = blog.order("-created")
            
        if blog.count() > 0:
            paginator = Paginator(blog, 10)
            p = paginator.page(page)
        else:
            p = []
        
        return p
    
    @classmethod
    def create(cls, title, content, category, sender):
        blog = cls(title=title, content=content, category=category, sender=sender)
        blog.put()
        return blog
        
    @classmethod
    def get_by_key(cls, key):
        blog = cls.get(key)
        blog.visited = blog.visited + 1
        blog.put()
        return blog
        
    
class BlogComment(db.Model):
    content = db.TextProperty(required=True)
    blog = db.ReferenceProperty(Blog, required=True)
    active = db.BooleanProperty(required=True, default=True)
    sender = db.ReferenceProperty(User, required=False, collection_name='blogcomment_set')
    created = db.DateTimeProperty(auto_now=False, auto_now_add=True)
    
    @classmethod
    def get_by_blog(cls, blog):
        query = cls.all()
        comments = query.filter("blog =", blog).order("-created").fetch(30)
        return comments
        
    @classmethod
    def create(cls, content, blog, sender=None):
        comment = cls(content=content, blog=blog, sender=sender)
        comment.put()
        return comment
        
        
class BlogForm(ModelForm):
    class Meta:
        model = Blog
        fields = ('title', 'content', 'category')
        
class BlogCommentForm(ModelForm):
    class Meta:
        model = BlogComment
        fields = ('content',)
        
    

